<?php
include('header.php');

$tw1 = mysql_query("SELECT * FROM `soundcloud` WHERE `user`='{$data['id']}' AND `active`='0'");
$tw = mysql_fetch_object($tw1);

if(isset($_POST['submit'])) {

$ext1 = mysql_query("SELECT * FROM `soundcloud` WHERE `s_name`='{$_POST['name']}' AND `user`='{$data['id']}'");
$ext = mysql_fetch_object($ext1);
if($ext->id != ""){
mysql_query("UPDATE `soundcloud` SET `active`='0' WHERE `s_name`='{$_POST['name']}' AND `user`='{$data['id']}'");
$mesaj = "<div class=\"success\">Soundcloud Account added successfully!</div>";
$h = 1;
}else{
$num1 = mysql_query("SELECT * FROM `soundcloud` WHERE `s_name`='{$_POST['name']}'");
$num = mysql_num_rows($num1);
if($num > 0){
$mesaj = "<div class=\"msg\"><div class=\"error\">Soundcloud Account Already Exists!</div></div>";
}else{ 
$sc = @file_get_contents("http://soundcloud.com/{$_POST['name']}");
$match = array(); 
preg_match('/class="user-image-large zoom photo" href="(.*?)" style=/',$sc,$match);
$datas['scimage'] = $match[1];
preg_match('/<meta content="(.*?)"\sproperty="og:url"/',$sc,$match);
$datas['scid'] = $match[1];
$exploder = explode("/", $datas['scid']);
 if ($exploder[6] != "")
{
mysql_query("INSERT INTO `soundcloud` (user, s_name, s_id, s_av) VALUES('{$data['id']}', '{$exploder[6]}', '{$exploder[6]}', '{$datas['scimage']}')");
$mesaj = "<div class=\"msg\"><div class=\"success\">Soundcloud Account added successfully! </div></div>";
$h = 1;
}else{
$mesaj = "<div class=\"msg\"><div class=\"error\">Soundcloud Account dosen't exist! </div></div>";
}}}}?>
<div class="block medium right">
			<div class="top"><?if(isset($data['id'])) {?>			
					<h1>Soundcloud Account</h1>
			</div>
		<div class="content"><? echo $mesaj; ?> 
<? if($_GET['a'] == "cpc"){
if(isset($_POST['editcpc'])){
mysql_query("UPDATE `soundcloud` SET `cpc`='{$_POST['cpc']}' WHERE `s_name`='{$tw->s_name}' AND `user`='{$data['id']}'");
echo "<div class=\"msg\"><div class=\"success\">CPC successfully changed!</div></div>";
}
?>
<form method="post">
<fieldset>
	<p>
		<label>CPC*</label> <br/>
        <select class="styled" name="cpc">
		    <option value="1">1 Coin</option>
			<option value="2" <?if($tw->cpc == 2){?>selected<?}?>>2 Coins</option>
			<option value="3" <?if($tw->cpc == 3){?>selected<?}?>>3 Coins</option>
			<option value="4" <?if($tw->cpc == 4){?>selected<?}?>>4 Coins</option>
			<option value="5" <?if($tw->cpc == 5){?>selected<?}?>>5 Coins</option>
			<option value="6" <?if($tw->cpc == 6){?>selected<?}?>>6 Coins</option>
			<option value="7" <?if($tw->cpc == 7){?>selected<?}?>>7 Coins</option>
			<option value="8" <?if($tw->cpc == 8){?>selected<?}?>>8 Coins</option>
			<option value="9" <?if($tw->cpc == 9){?>selected<?}?>>9 Coins</option>
			<option value="10" <?if($tw->cpc == 10){?>selected<?}?>>10 Coins</option>
		</select>
	</p>
	<p><small><b>* Coins per Click.</b></small></p>	
    <p>
		<input type="submit" class="button gray small" value="Change CPC" name="editcpc" />
	</p>
</fieldset>
</form>
<? }else if($tw->s_name != ""){?>
<div style="float:left; margin-left: 5px;"><img src="<? echo $tw->s_av;?>" border="0" /></div>
<div style="float:left; margin-left: 5px;"><b><a href="http://soundcloud.com/<? echo $tw->p_id;?>" style="text-decoration: underline; color: #000;" target="_blank"><? echo $tw->s_name;?></a></b> (<a href="delete_sc.php?id=<? echo $tw->id;?>" style="text-decoration: none; color: red;">Delete</a>)<br><b>Coins:</b> <? echo $tw->points;?> (<a href="addcoins.php?x=sc" style="text-decoration: none; color: blue;">Add</a>)<br><b>CPC:</b> <? echo $tw->cpc;?> (<a href="soundcloudconfig.php?a=cpc" style="text-decoration: none; color: blue;">Edit</a>)</div>
<?}else if(isset($_POST['submit']) && $h == 1){?>
<center><br><img src="<? echo $datas['scimage'];?>" border="0" /><br><b><? echo $_POST['name'];?></b></center><?}else{?>
<form method="post">
<table class="form">
	<tr>
		<td width="150px"><b>Soundcloud Username</b></td>
		<td>
			<input type="text" name="name" /><br />			
		</td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" name="submit" value="Submit" /></td>
	</tr>
</table>
<div class="msg"><div class="info">If you don't add your real Soundcloud Username, you can't earn coins.</div></div>
</form>
<?}}else{?><script>document.location.href='index.php'</script><?}?>
				</div>
			</div>		
<?include('footer.php');?>